using System;
using System.Collections.Generic;
using System.Text;
using Emlyn.Common.Tools.BusinessLayer;

namespace Emlyn.Website.Articles
{
    public class ArticleMod : BaseMod<Article, Guid, ArticleDataLayer>
    {
        public ArticleMod()
            : base(new ArticleDataLayer())
        {
        }

        public void AddArticleRelationship(Guid aParentArticleID, Guid aChildArticleID)
        {
            _dl.AddArticleRelationship(aParentArticleID, aChildArticleID);
        }

        public void RemoveArticleRelationship(Guid aParentArticleID, Guid aChildArticleID)
        {
            _dl.RemoveArticleRelationship(aParentArticleID, aChildArticleID);
        }

        /// <summary>
        /// This method moves a child up in the parent's article list
        /// </summary>
        public void MoveArticleRelationshipUp(Guid aParentArticleID, Guid aChildArticleID)
        {
            // algorithm:
            // Figure out the previous two articles, modify the order to place this article between them.

            IList<Article> aChildArticles = (new ArticleQuery()).GetArticlesForParentArticleID(aParentArticleID);

            int aChildPos;
            for (aChildPos = 0; aChildPos < aChildArticles.Count; aChildPos++)
            {
                if (aChildArticles[aChildPos].ArticleID == aChildArticleID)
                    break;
            }

            if (aChildPos < aChildArticles.Count && aChildPos > 0)
            {
                double lPrevArticleOrderNum = _dl.GetOrderNumForArticleRelationship(aParentArticleID, aChildArticles[aChildPos - 1].ArticleID);
                double lPrevPrevArticleOrderNum = 0.0;

                if (aChildPos > 1)
                {
                    lPrevPrevArticleOrderNum = _dl.GetOrderNumForArticleRelationship(aParentArticleID, aChildArticles[aChildPos - 2].ArticleID);
                };

                double lnewOrderNum = (lPrevArticleOrderNum + lPrevPrevArticleOrderNum) / 2;

                _dl.SetOrderNumForArticleRelationship(aParentArticleID, aChildArticleID, lnewOrderNum);
            }
        }

        /// <summary>
        /// This method moves a child up in the parent's article list
        /// </summary>
        public void MoveArticleRelationshipDown(Guid aParentArticleID, Guid aChildArticleID)
        {
            // algorithm:
            // Figure out the previous two articles, modify the order to place this article between them.

            IList<Article> aChildArticles = (new ArticleQuery()).GetArticlesForParentArticleID(aParentArticleID);

            int aChildPos;
            for (aChildPos = 0; aChildPos < aChildArticles.Count; aChildPos++)
            {
                if (aChildArticles[aChildPos].ArticleID == aChildArticleID)
                    break;
            }

            if (aChildPos < aChildArticles.Count - 1)
            {
                double lNextArticleOrderNum = _dl.GetOrderNumForArticleRelationship(aParentArticleID, aChildArticles[aChildPos + 1].ArticleID);
                double lNextNextArticleOrderNum = 0.0;

                if (aChildPos < aChildArticles.Count - 2)
                {
                    lNextNextArticleOrderNum = _dl.GetOrderNumForArticleRelationship(aParentArticleID, aChildArticles[aChildPos + 2].ArticleID);
                }
                else
                {
                    lNextNextArticleOrderNum = lNextArticleOrderNum + 2;
                }

                double lnewOrderNum = (lNextArticleOrderNum + lNextNextArticleOrderNum) / 2;

                _dl.SetOrderNumForArticleRelationship(aParentArticleID, aChildArticleID, lnewOrderNum);
            }
        }

        public void ReorderChildrenByLastDate(Guid aParentArticleID)
        {
            IList<Article> aChildArticles = (new ArticleQuery()).GetArticlesForParentArticleID(aParentArticleID);

            List<Article> aNewChildArticles = new List<Article>();

            foreach (Article larticle in aChildArticles)
                aNewChildArticles.Add(larticle);

            aNewChildArticles.Sort(CompareArticleByLastChanged);

            double lorderNum = 10.0;
            foreach (Article larticle in aNewChildArticles)
            {
                _dl.SetOrderNumForArticleRelationship(aParentArticleID, larticle.ArticleID, lorderNum);
                lorderNum += 10.0;
            }
        }

        public void ReorderChildrenByName(Guid aParentArticleID)
        {
            IList<Article> aChildArticles = (new ArticleQuery()).GetArticlesForParentArticleID(aParentArticleID);

            List<Article> aNewChildArticles = new List<Article>();

            foreach (Article larticle in aChildArticles)
                aNewChildArticles.Add(larticle);

            aNewChildArticles.Sort(CompareArticleByArticleName);

            double lorderNum = 10.0;
            foreach (Article larticle in aNewChildArticles)
            {
                _dl.SetOrderNumForArticleRelationship(aParentArticleID, larticle.ArticleID, lorderNum);
                lorderNum += 10.0;
            }
        }

        public int CompareArticleByLastChanged(Article x, Article y)
        {
            return x.LastChanged < y.LastChanged? 1 : (x.LastChanged == y.LastChanged? 0: -1);
        }

        public int CompareArticleByArticleName(Article x, Article y)
        {
            return x.ArticleName.CompareTo(y.ArticleName);
        }
    }



//<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
//    CommandName="Delete" Text="Delete"
//    OnClientClick="return confirm('Are you certain you want to delete this product?');">
//</asp:LinkButton> 

}
